const ajax = {
    get(url, data) {
        return new Promise((resolve, reject) => {
            myAjax(url, data, "get", resolve, reject);
        })
    },
    post(url, data) {
        return new Promise((resolve, reject) => {
            myAjax(url, data, "post", resolve, reject);
        })
    },
    delete(url, data) {
        return new Promise((resolve, reject) => {
            myAjax(url, data, "delete", resolve, reject);
        })
    },
}

function myAjax(url, data, method, successFun, errorFun) {
    $.ajax({
        url,
        method,
        data,
        success(res) {
            if (res.code === 200) {
                successFun(res.data);
            } else if (res.code === 401) {
                layer.alert(res.msg, {icon: 5}, function (index) {
                    top.location.replace("/");
                    layer.close(index);
                })
                errorFun(res);
            } else {
                layer.alert(res.msg, {icon: 5})
                errorFun(res);
            }
        },
        error(e) {
            console.log(e);
            layer.alert("与服务器断开连接...");
            errorFun(e);
        }
    })
}
